﻿using System;
using System.Collections.Generic;
using System.Text;
using AdaptiveAgents.Agents;
using System.Data;
using AdaptiveAgents.Loggers;
using AdaptiveAgents.Results;

namespace AdaptiveAgents.Games
{
    class NormalGameToCSV:Game
    {
        //data members
        private DataRow dr1;
        //private DataRow dr2;

        /// <summary>
        /// Getter and Setter for the first DataRow
        /// Usually - for saving the utility of the round
        /// </summary>
        public DataRow Dr1
        {
            get { return dr1; }
            set { dr1 = value; }
        }

        /// <summary>
        /// Getter and Setter for the second DataRow
        /// Usually - for saving the active player of the round
        /// (not always needed!)
        /// </summary>
        //public DataRow Dr2
        //{
        //    get { return dr2; }
        //    set { dr2 = value; }
        //}

        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="env">the environment</param>
        /// <param name="numOfRounds">number of rounds in the game</param>
        public NormalGameToCSV(Environment env, int numOfRounds): base(env, numOfRounds)
        { }

        /// <summary>
        /// Decides which player is the next player
        /// </summary>
        /// <param name="activePlayer"></param>
        /// <returns>ID of the next player according to active player decision</returns>
        protected override int chooseNextPerformer(int activePlayer)
        {
            return _environment.agents[activePlayer].chooseNextPerformer();
        }        

        /// <summary>
        /// Make a move by current active player.
        /// </summary>
        /// <param name="round">The round that current active player plays in</param>
        /// <param name="activePlayer">Current active player</param>
        /// <param name="turn">Used for keep tracking of turn data</param>
        /// <param name="accumUtility">Total utility until current turn</param>
        protected override void makeMove(int round, int activePlayer, Turn turn, ref double accumUtility)
        {
            //Get the performance of the active player
            double performance = _environment.agents[activePlayer].performNow();

            //Who is the active player
            turn.ActivePlayer = activePlayer;

            //Accumulate the performance of the active player
            accumUtility += performance;
            //dr1["utility " + round.ToString()] = performance;

            //Active player performance
            turn.Performence = performance;

            //Add a new observation of current turn by all players
            foreach (Agent agent in _environment.agents)
                agent.addObservation(new Observation(round, _environment.agents[activePlayer].ID, performance));

            //Estimate competence of each performer
            for (int i = 0; i < _environment.agents.Count; ++i)
            {
                int otherAgent = (i + 1) % _environment.agents.Count; //ID of other agent than current agent
                turn.addEstimatedCompetence(_environment.agents[otherAgent].getTeammateModel(i).EstimateCompetence());
            }
        }
    }

    
}
